from rest_framework import serializers
from ..models.Author import Author
from ..models.Keyword import Keyword
from ..models.Department import Department
from ..models.Institution import Institution
from ..serializers.keywordListSerializer import TagListSerializer
from ..serializers.instInfoSerializer import InstInfoSerializer
from ..serializers.departInfoSerializer import DepInfoSerializer

class AuthorCreateSerializer(serializers.ModelSerializer):
    tag = serializers.PrimaryKeyRelatedField(many=True, queryset=Keyword.objects.all(), required=False)
    institutions = serializers.PrimaryKeyRelatedField(many=True, queryset=Institution.objects.all(), required=False)
    departments = serializers.PrimaryKeyRelatedField(many=True, queryset=Department.objects.all(), required=False)
    
    class Meta:
        model = Author
        fields = ["name", "zh_name", "orcid", "email", "country", "city", "address", "phone", "education_degree", \
                  "research_field", "homepage", "description", "tag", "institutions", "departments"]
    
    
    def create(self, validated_data):
        # 从数据中移除 authors 数据，因为它不能直接通过 **validated_data 进行创建
        tag_data = validated_data.pop('tag', [])
        institutions_data = validated_data.pop('institutions', [])
        departments_data = validated_data.pop('departments', [])
        a = Author.objects.create(**validated_data)

        # 使用 .set() 方法设置多对多关系
        a.tag.set(tag_data)
        a.institutions.set(institutions_data)
        a.departments.set(departments_data)

        return a
    
    def update(self, instance, validated_data):
        tag_data = validated_data.pop('tag', [])
        institutions_data = validated_data.pop('institutions', [])
        departments_data = validated_data.pop('departments', [])

        # 更新实例的其他字段
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()
        
        instance.tag.set(tag_data)
        instance.institutions.set(institutions_data)
        instance.departments.set(departments_data)

        return instance
    

class AuthorInfoSerializer(serializers.ModelSerializer):
    tag = serializers.SerializerMethodField()
    institutions = serializers.SerializerMethodField()
    departments = serializers.SerializerMethodField()
    
    class Meta:
        model = Author
        fields = ["uuid", "name", "zh_name", "orcid", "email", "country", "city", "address", "phone", "education_degree", \
                  "research_field", "homepage", "description", "tag", "institutions", "departments"]
        
    def get_institutions(self, obj):
        institution_list = []
        for institution in obj.institutions.all():
            institution_list.append(InstInfoSerializer(institution).data)
        return institution_list
    
    def get_tag(self, obj):
        tag_list = []
        for tag in obj.tag.all():
            tag_list.append(TagListSerializer(tag).data)
        return tag_list
    
    def get_departments(self, obj):
        department_list = []
        for department in obj.departments.all():
            department_list.append(DepInfoSerializer(department).data)
        return department_list